package com.android.boncen.commonFrame.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.adapter.Knowledge_viewpagerAdapter;
import com.android.boncen.commonFrame.base.BaseFragment;
import com.android.boncen.commonFrame.base.Knowledge_baseFrament;
import com.android.boncen.commonFrame.bean.KnowledgeBean;
import com.google.gson.Gson;
import com.show.api.ShowApiRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RunnableFuture;

public class knowledgeFragment extends BaseFragment {

    private TextView title;     //此fragment的标题
    private  View rootView;

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private String json;

    KnowledgeBean klgBean;
    ArrayList<Knowledge_baseFrament> fragments;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Override
    protected View initView() {

        if(rootView == null){
            rootView = View.inflate(getContext(), R.layout.knowledge_fragment,null);
        }
        title = (TextView)rootView.findViewById(R.id.tv_title);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout);
        viewPager = (ViewPager) rootView.findViewById(R.id.knowledge_viewpage);
        fragments = new ArrayList<>();

        getKnowledgeJson(); //拿到知识分类的json
        SystemClock.sleep(500);
        klgBean = handleJson(json);            //拿到知识分类的bean对象
            for (int i = 0; i < klgBean.getShowapi_res_body().getList().size()-1 ; i++) {
                fragments.add(new Knowledge_baseFrament(mContext,klgBean.getShowapi_res_body().getList().get(i).getName(),klgBean.getShowapi_res_body().getList().get(i).getId()));
            }



        Knowledge_viewpagerAdapter adapter = new Knowledge_viewpagerAdapter(getFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        return rootView;
    }

    private void getKnowledgeJson() {
        new Thread(){
            @Override
            public void run() {
                String appid = "31257";
                String secret = "0ad89f99ef1041f1bcfb14689bfe84ba";
              json = new ShowApiRequest("http://route.showapi.com/90-86",appid,secret).post();
            }
        }.start();
    }

    public KnowledgeBean handleJson(String json){
        return new Gson().fromJson(json,KnowledgeBean.class);
    }
    @Override
    protected void initData() {
        super.initData();
        title.setText("养生知识");

    }
}
